//
//  EMRequestModel.m
//  EasyMoney
//
//  Created by zh on 2019/12/14.
//  Copyright © 2019 zh. All rights reserved.
//

#import "EMRequestModel.h"

@implementation EMRequestModel

- (NSString *)responseClassName
{
    if (!_responseClassName) {
        NSString* className = NSStringFromClass(self.class);
        _responseClassName = [className stringByReplacingOccurrencesOfString:@"RequestModel" withString:@"ResponseModel"];
    }
    return _responseClassName;
}

- (void)requestSuccess:(RequestSuccessBlock)success failure:(RequestFailureBlock)failure {
    if (!self.requestMethodKey || !self.requestMethodKey.length) return;

    EMNetClient *client = [EMNetClient sharedClient];
    [client requestWithMethodKey:self.requestMethodKey isPost:self.isPostForRequest param:[self params] success:^(id responseObject, NSString* message) {
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            Class responseClass = NSClassFromString(self.responseClassName);
            id respObj = [responseClass yy_modelWithJSON:responseObject];
            if (success) {
                success(respObj, message);
            }
        }
        else
        {
            if (success) {
                success(responseObject, message);
            }
        }

    } failed:^(NSInteger errorCode, NSString *errorMessage, id errorData) {
        if (failure)
        {
            failure(errorCode, errorMessage, errorData);
        }
    }];
}

- (void)uploadConstructBlock:(void (^)(id <AFMultipartFormData> formData))constructBlock progress:(void(^)(NSProgress *))progress success:(RequestSuccessBlock)success failure:(RequestFailureBlock)failure
{
    EMNetClient *client = [EMNetClient sharedClient];
    [client uploadWithMethodKey:self.requestMethodKey constructBlock:constructBlock param:[self params] progress:progress success:^(id responseObject, NSString *message) {

        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            Class responseClass = NSClassFromString(self.responseClassName);
            id respObj = [responseClass yy_modelWithJSON:responseObject];
            if (success) {
                success(respObj,message);
            }
        }
    } failed:^(NSInteger errorCode, NSString *errorMessage, id errorData) {
        if (failure)
        {
            failure(errorCode, errorMessage, errorData);
        }
    }];
}

- (NSDictionary *)params {
    NSDictionary *json = self.yy_modelToJSONObject;
    if ([json isKindOfClass:[NSDictionary class]]) {
        NSMutableDictionary *params = [(NSDictionary *)json mutableCopy];
        //移除super的3个属性
        [params removeObjectsForKeys:@[@"requestMethodKey", @"isPostForRequest",@"responseClassName"]];
        //移除自己为nil的属性
        [params enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
            if (!obj) {
                [params removeObjectForKey:key];
            }
        }];

        return params;
    }
    return nil;
}

@end
